package com.palm.hubo2o.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.palm.hubo2o.R;
import com.palm.hubo2o.model.ScrollerAction;
import com.palm.palmutils.widget.CircleIndicator;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by dest1 on 2015/9/18.
 */
public class HomeHeader extends FrameLayout implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager mViewPager;
    private CircleIndicator mIndicator;
    private List<ImageView> mListImages;
    private HeadHandler mHandler;

    private boolean autoScroll;

    private final static int SCROLL_WHAT = 0;
    private final static long SCROLL_DELAY = 5000;

    public HomeHeader(Context context) {
        super(context);
        init(context);
    }

    public HomeHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HomeHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public HomeHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_home_header, this);
        mViewPager = (ViewPager) findViewById(R.id.vp_header);
        mIndicator = (CircleIndicator) findViewById(R.id.indicator_header);
        mHandler = new HeadHandler(mViewPager);
        mListImages = new ArrayList<>();
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int width = getMeasuredWidth();
        final int height = (int) (width / 64f * 27f);
        setMeasuredDimension(width, height);
    }

    public void setGallery(List<ScrollerAction> acts) {
        mListImages.clear();
        for (ScrollerAction act : acts) {
            ImageView img = new ImageView(getContext());
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(-1, -1);
            img.setLayoutParams(lp);
            Glide.with(getContext()).load(act.getImage_url()).into(img);
//            img.setTag(act.h5webapp);
            mListImages.add(img);
        }
        if (mViewPager.getAdapter() == null)
            mViewPager.setAdapter(adapter);
        else
            adapter.notifyDataSetChanged();
        mIndicator.setViewPager(mViewPager);
    }


    private PagerAdapter adapter = new PagerAdapter() {

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mListImages.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View v = mListImages.get(position);
            v.setOnClickListener(HomeHeader.this);
            container.addView(v);
            return v;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return mListImages.size();
        }
    };

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (autoScroll) {
            mHandler.removeMessages(SCROLL_WHAT);
            mHandler.sendEmptyMessageDelayed(SCROLL_WHAT, SCROLL_DELAY);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    public void onClick(View v) {
//        Intent intent = new Intent(mContext, WebActivity.class);
//        intent.putExtra(Constant.URL_TAG, v.getTag().toString());
//        getContext().startActivity(intent);

    }

    public void startAutoScroll() {
        if (mListImages.size() == 0 || autoScroll)
            return;
        mHandler.removeMessages(SCROLL_WHAT);
        mHandler.sendEmptyMessageDelayed(SCROLL_WHAT, SCROLL_DELAY);
        autoScroll = true;
    }

    public void stopAutoScroll() {
        mHandler.removeMessages(SCROLL_WHAT);
        autoScroll = false;
    }

    private static class HeadHandler extends Handler {
        private final WeakReference<ViewPager> weakReference;

        public HeadHandler(ViewPager viewPager) {
            weakReference = new WeakReference<>(viewPager);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ViewPager pager = weakReference.get();
            if (pager != null)
                switch (msg.what) {
                    case SCROLL_WHAT:
                        int current = pager.getCurrentItem();
                        if (current == pager.getAdapter().getCount() - 1)
                            pager.setCurrentItem(0);
                        else
                            pager.setCurrentItem(current + 1);
                        break;
                }
        }
    }

}